#责任链模式

#一、责任链模式的优点和应用场景
#优点：
#1、将请求者与处理者分离，请求者并不知道请求是被哪个处理者所处理，易于扩展。

#应用场景：
#1、若一个请求可能由一个对请求有链式优先级的处理群所处理时，可以考虑责任链模式。除本例外，银行的客户请求处理系统也可以用责任链模式实现（VIP客户和普通用户处理方式当然会有不同）。

#二、责任链模式的缺点
#1、如果责任链比较长，会有比较大的性能问题；2、如果责任链比较长，若业务出现问题，比较难定位是哪个处理者的问题。


class manager():
    successor = None
    name = ''

    def __init__(self, name):
        self.name = name

    def setSuccessor(self, successor):
        self.successor = successor

    def handleRequest(self, request):
        pass


class lineManager(manager):
    def handleRequest(self, request):
        if request.requestType == 'DaysOff' and request.number <= 3:
            print ('%s:%s Num:%d Accepted OVER' % (self.name, request.requestContent, request.number))

        else:
            print ('%s:%s Num:%d Accepted CONTINUE' % (self.name, request.requestContent, request.number))

            if self.successor != None:
                self.successor.handleRequest(request)


class departmentManager(manager):
    def handleRequest(self, request):
        if request.requestType == 'DaysOff' and request.number <= 7:
            print ('%s:%s Num:%d Accepted OVER' % (self.name, request.requestContent, request.number))

        else:
            print ( '%s:%s Num:%d Accepted CONTINUE' % (self.name, request.requestContent, request.number))

            if self.successor != None:
                self.successor.handleRequest(request)


class generalManager(manager):
    def handleRequest(self, request):
        if request.requestType == 'DaysOff':
            print ('%s:%s Num:%d Accepted OVER' % (self.name, request.requestContent, request.number))



class request():
    requestType = ''
    requestContent = ''
    number = 0


if __name__ == "__main__":
    line_manager = lineManager('LINE MANAGER')
    department_manager = departmentManager('DEPARTMENT MANAGER')
    general_manager = generalManager('GENERAL MANAGER')

    line_manager.setSuccessor(department_manager)
    department_manager.setSuccessor(general_manager)

    req = request()
    req.requestType = 'DaysOff'
    req.requestContent = 'Ask 1 day off'
    req.number = 1
    line_manager.handleRequest(req)

    req.requestType = 'DaysOff'
    req.requestContent = 'Ask 5 days off'
    req.number = 5
    line_manager.handleRequest(req)

    req.requestType = 'DaysOff'
    req.requestContent = 'Ask 10 days off'
    req.number = 10
    line_manager.handleRequest(req)

